﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows;
using FirstFloor.ModernUI.Windows.Controls;
using Lephone.Data;
using Lephone.Data.Definition;
using Microsoft.Win32;

namespace SimlvClient.Pages
{
    /// <summary>
    /// Interaction logic for NewsDetail.xaml
    /// </summary>
    public partial class NewsDetail : ModernDialog
    {
        public Model.News CurrentModel { get; set; }

        public string ButtonContent = "浏览($)";
        public int ImageCount = 0;
        public NewsDetail(Model.News Model)
        {
            char ss = (char)5;
            this.CurrentModel = Model;
            InitializeComponent();
            Buttons = new[] {
               OkButton, CancelButton
            };
            OkButton.Click += OkButton_Click;
            GetData(Model);
        }

        void OkButton_Click(object sender, RoutedEventArgs e)
        {
            string NContent = frame.InvokeScript("getHtml").ToString();
            this.CurrentModel.NContent = NContent;
            this.CurrentModel.Title = this.txtTitle.Text;
            this.CurrentModel.ModifyTime = Date.Now.ToString();
            this.CurrentModel.From = txtFrom.Text;
            this.CurrentModel.Save();
            Model.OperPageHistory ModelHistory = new Model.OperPageHistory();
            ModelHistory.CateId = this.CurrentModel.CateId;
            if (this.CurrentModel.CateId == 0)
            {
                ModelHistory.IsSimple = 1;
            }
            else
            {
                ModelHistory.IsSimple = 0;
            }
            if (CurrentModel.Id == 0)
            {
                ModelHistory.OperName = "Add";
            }
            else
            {
                ModelHistory.OperName = "Edit";
            }
            ModelHistory.DataId = this.CurrentModel.Id;
            ModelHistory.Save();
        }

        private void BrownImg(object sender, RoutedEventArgs e)
        {

            if (CurrentModel != null)
            {
                ImgList ImgListPage = new ImgList(CurrentModel.NewsImg.ToList());
                if (ImgListPage.ShowDialog().Value)
                {
                    var NewsModel = DbEntry.From<SimlvClient.Model.News>().Where(o => o.Id == CurrentModel.Id).OrderByDescending(o => o.Id).Select().SingleOrDefault();
                    CurrentModel = NewsModel;
                    txt.Content = ButtonContent.Replace("$", ImgListPage.NewImageModel.Count().ToString());
                }
                else
                {
                    var NewsModel = DbEntry.From<SimlvClient.Model.News>().Where(o => o.Id == CurrentModel.Id).OrderByDescending(o => o.Id).Select().SingleOrDefault();
                    CurrentModel = NewsModel;
                    txt.Content = ButtonContent.Replace("$", ImgListPage.NewImageModel.Count().ToString());
                }
            }

        }

        public void GetData(Model.News Model)
        {
            ImageCount = Model.NewsImg.Count();
            txt.Content = ButtonContent.Replace("$", ImageCount.ToString());
            this.txtTitle.Text = Model.Title;
            this.txtFrom.Text = Model.From;
            Basic bs = new Basic();
            bs.Name = Model.NContent;
            this.frame.Source = new Uri(Directory.GetCurrentDirectory() + "/edit/simple.html", UriKind.Absolute);
            frame.ObjectForScripting = bs;

        }

        private void UploadCommand(object sender, RoutedEventArgs e)
        {

            Thread thread = new Thread(new ThreadStart(UploadtoServer));
            thread.Start();
        }
        private delegate void DelegateSetCurrentTime();
        private void UploadtoServer()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            Nullable<bool> result = dlg.ShowDialog();
            if (result.Value)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    IsBusy.IsIndeterminate = true;
                    LayoutRoot.IsEnabled = false;
                    OkButton.IsEnabled = false;
                    CancelButton.IsEnabled = false;
                }));
                string filename = dlg.FileName;

                string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
                string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"]; ;
                string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
                string ImageFolder = ConfigurationManager.AppSettings["NewsImage"];
                if (ImageFolder != "")
                {
                    ImageFolder = ImageFolder + "/";
                }
                FTPHelper fhelper = new FTPHelper(ftpServerIP, ftpUserID, ftpPassword);
                string ImageGuid = ImageFolder + Guid.NewGuid().ToString();
                //NewsImage
                string ImageUrl = ConfigurationManager.AppSettings["website"] + ImageGuid + ".jpg";


             

                if (fhelper.Upload(filename, ImageGuid + ".jpg"))
                {
                    //save in db
                   


                    //Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.SystemIdle, new DelegateSetCurrentTime(ShowInUI));
                    Dispatcher.Invoke(new Action(() =>
                    {
                        string NContent = frame.InvokeScript("getHtml").ToString();
                        this.CurrentModel.NContent = NContent;
                        this.CurrentModel.Title = this.txtTitle.Text;
                        this.CurrentModel.ModifyTime = Date.Now.ToString();
                        this.CurrentModel.From = txtFrom.Text;
                        Model.NewsImg ImgModel = new Model.NewsImg();
                        //ImgModel.NewId = this.CurrentModel.Id;
                        ImgModel.News = this.CurrentModel;
                        ImgModel.ImageUrl = ImageUrl;
                        ImgModel.Save();
                        IsBusy.IsIndeterminate = false;
                        LayoutRoot.IsEnabled = true;
                        OkButton.IsEnabled = true;
                        CancelButton.IsEnabled = true;
                        txt.Content = ButtonContent.Replace("$", (ImageCount + 1).ToString());
                        ModernDialog.ShowMessageText("上传成功！");
                    }));
                }
            }
        }



    }

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问  
    public class Basic
    {
        public static string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string ClickEvent()
        {
            return Name;
        }
    }
}
